#include "../../MyUtils/GLogHelper.hpp"
#include "../../MyUtils/Net/Server.hpp"
#include "../../MyUtils/Timer/ProcessTimer.hpp"
#include <iostream>
#include <thread>
using namespace std;

static void _socket_read_cb( bufferevent *bev, void *arg )
{
    static MyUtils::MyTimer::ProcessTimer _timer;
    static long count = 0;
    if(++count % 100000 == 0)
    {
        printf("count: %d\n", count);
        printf("time: %lds\n", _timer.duration() / 1000);
        printf("count/s: %lf\n", count/(_timer.duration() / 1000.0));
        printf("\n");
    }
    char msg[ 4096 ];
    size_t len = bufferevent_read( bev, msg, sizeof( msg ) - 1 );
    msg[ len ] = '\0';
    char reply[] = "I has read your data";
    int ret = bufferevent_write( bev, reply, strlen( reply ) );
    return;
}

static void _socket_event_cb( bufferevent *bev, short events, void *arg )
{
    if ( events & BEV_EVENT_EOF )
    {
        MYLOG_INFO( "connection closed\n" );
    }
    else if ( events & BEV_EVENT_ERROR )
    {
        MYLOG_ERROR( "some other error\n" );
    }
    //这将自动close套接字和free读写缓冲区
    bufferevent_free( bev );
}

int main( )
{
    MyUtils::GlogHelper helper( false );

    MyUtils::Net::Server server(
        9999, 100000000000,
        []( evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sock,
            int socklen, void *arg ) {
            printf( "accept a client %d\n", fd );
            event_base *base = ( event_base * ) arg;
            //为这个客户端分配一个bufferevent
            bufferevent *bev = bufferevent_socket_new(
                base, fd, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE );
            bufferevent_setcb( bev, _socket_read_cb, NULL, _socket_event_cb,
                               NULL );
            bufferevent_enable( bev, EV_READ | EV_PERSIST );
        } );

    std::thread server_thread( [ & ]( ) { server.run( ); } );

    server_thread.join( );

    return 0;
}